/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 *
 * @author admin
 */
public class PanelTambahFotoAnggota extends JPanel {
    private JLabel labelKodeAnggota;
    private JTextField textKodeAnggota;
    private JButton tombolSimpan;
    private JButton tombolCariGambar;
    private File file=null;
    private BufferedImage image;
    private JPanel panelTombol;
    
    public PanelTambahFotoAnggota(){
        try {
            inisialisasi();
            this.file = new File("Sunset.jpg");
            image = ImageIO.read(file);
            image.getGraphics();
        } catch (IOException ex) {
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void inisialisasi(){
        
        setLayout(new BorderLayout());
        panelTombol = new JPanel();
        panelTombol.setLayout(new FlowLayout());
        
        labelKodeAnggota = new JLabel("Kode");
        panelTombol.add(labelKodeAnggota);
        
        textKodeAnggota= new JTextField();
        textKodeAnggota.setColumns(10);
        panelTombol.add(textKodeAnggota);
        textKodeAnggota.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                textKodeAnggotaAction();
            }

            public void textKodeAnggotaAction() {
                try {
                    String kode = textKodeAnggota.getText();
                    String namafile = kode + ".jpg";
                    File fileNew = new File(namafile);
                    image = ImageIO.read(fileNew);
                    repaint();
                }
                catch (IOException ex) {
                    Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        tombolCariGambar=new JButton("CariGambar");
        panelTombol.add(tombolCariGambar);
        tombolCariGambar.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tombolCariGambarAction();
            }
        });
        
        tombolSimpan=new JButton("Simpan");
        panelTombol.add(tombolSimpan);
        tombolSimpan.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tombolSimpanAction();
            }

            private void tombolSimpanAction() {
                try {
                    String kode = textKodeAnggota.getText();
                    BufferedImage img = ImageIO.read(file);
                    BufferedImage image = convertBuffered(img);
                    simpan(image, kode, "jpg");
                    JOptionPane.showMessageDialog(null, "Image tersimpan");
                } catch (IOException ex) {
                    Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            private BufferedImage convertBuffered(BufferedImage img) {
                int x = img.getWidth(null);
                int y = img.getHeight(null);
                int cetak = BufferedImage.TYPE_INT_RGB;
                BufferedImage buf = new BufferedImage(x, y, cetak);
                Graphics2D graf = buf.createGraphics();
                graf.drawImage(img, 0, 0, null);
                graf.dispose();
                return buf;
            }

            private void simpan(BufferedImage image, String kode, String ext) {
                String kodeAng = kode;
                File file = new File(kodeAng + "." + ext);
                try {
                    ImageIO.write(image, ext, file);
                } catch (IOException ex) {
                    Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } );

        add(panelTombol, BorderLayout.SOUTH);
    }
    
    @Override
    public void paintComponent(Graphics g){

        g.drawImage(image,
        0, 0, 400, 300,
        0, 0, image.getWidth(null), image.getHeight(null),
        null);
    }
    
    public void tombolCariGambarAction(){

        JFileChooser chooser = new JFileChooser();
        int status = chooser.showOpenDialog(chooser);
        if(status == JFileChooser.APPROVE_OPTION){
            try {
                file = chooser.getSelectedFile();
                image = ImageIO.read(file);
                repaint();
            } catch (IOException ex) {
                Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
